package org.example;

import org.example.Enum.ActionEnum;
import org.example.Enum.Conditions;
import org.example.Enum.PanelState;

import java.util.ArrayList;

/**
 * @program: B2_exper4
 * @description:
 * @create: 2023-11-05 08:57
 **/
public class StateMachine {
    private ArrayList<StateTranslation> stateTranslations;
    private PanelState currentState;

    public StateMachine(PanelState currentState){
        stateTranslations = new ArrayList<>();
        this.currentState = currentState;
    }

    public PanelState getCurrentState() {
        return currentState;
    }

    public StateTranslation addTranslation(){
        StateTranslation stateTranslation = new StateTranslation();
        stateTranslations.add(stateTranslation);
        return stateTranslation;
    }

    public StateTranslation execute(ActionEnum action, Conditions condition){
        for (StateTranslation translation : stateTranslations){
//            System.out.println("translations: " + translation.getCurrentState());
            if (translation.getCurrentState() == currentState
                && translation.getAction() == action
                && (translation.getCondition() == condition || translation.getCondition() == null || condition == null)){
                translation.getEvent().handle();
                currentState = translation.getNextState();
                return translation;
            }
        }
        return null;
    }
}
